package com.eimf.saf.base.querycomm;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;

import com.eimf.saf.base.querycomm.annotation.FileNameAnnotation;

/**
 * @author eden
 * @date 2013-11-18 下午4:47:45
 * @Description: 组装页面传递过来的查询条件
 */
public class GenerateSearchUtil
{
    /**
     * 
     * 根据页面传递的参数组装查询内容
     * 
     * @param aClazz
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException SearchExpression
     * @throws NoSuchMethodException
     * @throws SecurityException
     * @throws InvocationTargetException
     * @exception
     * @since 1.0.0
     */
    public static SearchExpression buildSearchExp(final Object objectCla) throws Exception
    {
        final SearchExpression searchExpression = new SearchExpression();
        final Field[] declaredFields = objectCla.getClass().getDeclaredFields();
        String fieldName = "";
        String[] queryContent;
        final HashMap<String, Object> betweenHash = new HashMap<String, Object>();
        for (final Field field : declaredFields)
        {
            fieldName = field.getName();
            queryContent = fieldName.split("_");
            if (fieldName.startsWith("query_"))
            {
                Expression temp = null;
                fieldName = field.getAnnotation(FileNameAnnotation.class).name();
                final Method m = objectCla.getClass().getMethod("getQuery_" + queryContent[1] + "_" + queryContent[2]);
                final String value = (String) m.invoke(objectCla);
                if (queryContent.length == 3)
                {
                    temp = new Expression(fieldName, value, OperatorEnum.getEnum(queryContent[1]));
                } else
                {
                    if (betweenHash.containsKey(fieldName)) // 如果能从hashmap中获取到对应的值，则进行组装between条件，否则放入hashmap
                    {
                        if (queryContent[2].equalsIgnoreCase("start"))
                        {
                            temp = new Expression(fieldName, value, betweenHash.get(fieldName));
                        } else
                        {
                            temp = new Expression(fieldName, betweenHash.get(fieldName), value);
                        }
                        betweenHash.remove(fieldName);
                    } else
                    {
                        betweenHash.put(fieldName, value);
                    }

                }
                searchExpression.addExpression(temp);
            } else if (fieldName.startsWith("order_"))
            {
                final Method m = objectCla.getClass().getMethod("getQuery_" + queryContent[1] + "_" + queryContent[2]);
                final String value = (String) m.invoke(objectCla);
                fieldName = field.getAnnotation(FileNameAnnotation.class).name();
                final OrderExpression orderExp = new OrderExpression(fieldName, Boolean.getBoolean(value));
                searchExpression.addOrderExpression(orderExp);
            }
        }
        return searchExpression;
    }
}
